package com.sihai.ehr.web.action.exam;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.Duty;
import com.sihai.ehr.model.biz.ExamBaseConfig;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.DutyService;
import com.sihai.ehr.service.ExamBaseConfigService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;
import com.sihai.ehr.web.pojo.ExamBaseConfigPojo;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.Iterator;
import java.util.List;

/**
 * @author wxn
 * @since 12-10-12 21:58
 * 考核对象设置action
 */
@Results({
        @Result(name = "qual_target_list", location = "exam_qual_target_list.jsp"),
        @Result(name = "main", location = "exam_qual_target_main.jsp")
})
public class ExamQualTargetAction extends EHRAction {
    private List<Organization> organizations;

    @BeanInject
    private OrganizationService organizationService;
    @BeanInject
    private ExamBaseConfigService examBaseConfigService;

    private LimitedList<ExamBaseConfigPojo> examBaseConfigPojos;

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 7204414972672241268L;

    @Action("/exam/qual_target_main")
    @Override
    public String execute() throws Exception {
        listAction = "/exam/qual_target_list.xhtml";
        listTitle = "考核对象设置";
        showChildOrg = true;
        organizations = organizationService.findChildren(null, true, null, null);
        if (organizations != null && organizations.size() > 0) {
        	if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
            organizationId = organizations.get(0).getId();
        }
        return "main";
    }

    //考核对象设置-列表
    @Action("/exam/qual_target_list")
    public String list() throws Exception {

        examBaseConfigPojos = examBaseConfigService.searchQualConfigList(organizationId, NumberUtils.createLong(getParameter("dutyId")), createLimit(), createSorter());

        //2013-06-19:修改员工考核管理-考核对象设置中筛选后添加审核人员时只显示当前部门人员资料的bug
        if(organizationId != null && organizationId > 0){
            Organization o = organizationService.get(organizationId);
            if(o != null){
                organizationId = o.getTopOrganization() != null ? o.getTopOrganization().getId() : o.getId();
            }
        }

        /* 分页 */
        this.paging(examBaseConfigPojos);
        /* 设置翻页action */
        this.setPageUrl("exam/qual_target_list.xhtml");
        
        return "qual_target_list";
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }
    
    public void setOrganizations(List<Organization> organizations) {
		this.organizations = organizations;
	}

    public LimitedList<ExamBaseConfigPojo> getExamBaseConfigPojos() {
        return examBaseConfigPojos;
    }

    public void setExamBaseConfigPojos(
			LimitedList<ExamBaseConfigPojo> examBaseConfigPojos) {
		this.examBaseConfigPojos = examBaseConfigPojos;
	}
}
